package org.bronycorps.mlptss.domain.model;

import java.util.ArrayList;
import java.util.List;

import org.bronycorps.mlptss.domain.model.event.StateChangeEvent;
import org.bronycorps.mlptss.domain.model.event.StateChangeEventListener;
import org.bronycorps.mlptss.manager.preferences.AssetsConfiguration.BossConfiguration;
import org.bronycorps.mlptss.utils.AnimationCache;
import org.bronycorps.mlptss.utils.DrawableAnimation;

import com.badlogic.gdx.math.Vector2;

public class Boss extends AbstractActorModel {

	public enum State {
		EXPLODE, NORMAL
	}

	private String animationDefault;

	private List<StateChangeEventListener> changeEventListeners = new ArrayList<StateChangeEventListener>();
	private int life;
	private final Player owner;
	private State state = State.NORMAL;

	private List<Vector2> waypoints = new ArrayList<Vector2>();

	public Boss(BossConfiguration bossConfig, Player owner) {
		super();
		this.owner = owner;
		this.life = 10;
		this.animationDefault = bossConfig.getAnimationDefault();

	}

	public boolean addListener(StateChangeEventListener e) {
		return this.changeEventListeners.add(e);
	}

	private void fireStateChange() {
		for (StateChangeEventListener listener : this.changeEventListeners) {
			listener.onStateChange(new StateChangeEvent(this));
		}
	}

	public DrawableAnimation getDefaultAnimation() {
		return AnimationCache.getInstance().get(this.animationDefault);
	}

	public int getLife() {
		return this.life;
	}

	public Player getOwner() {
		return this.owner;
	}

	public State getState() {
		return this.state;
	}

	public List<Vector2> getWaypoints() {
		return this.waypoints;
	}

	/**
	 * hit by a bullet
	 * 
	 * @param model
	 */
	public void hit(Bullet model) {
		hit(1);
	}

	private void hit(int damage) {
		if (this.state == State.NORMAL) {
			this.life--;
			if (this.life <= 0) {
				setState(State.EXPLODE);
			}
		}
	}

	public boolean isExploding() {
		return (this.state == State.EXPLODE);
	}

	public boolean remove(StateChangeEventListener o) {
		return this.changeEventListeners.remove(o);
	}

	public void setLife(int life) {
		this.life = life;
	}

	public void setState(State state) {
		this.state = state;
		fireStateChange();
	}

	public void setWaypoints(List<Vector2> waypoints) {
		this.waypoints = waypoints;
	}

	public void terminate() {
		setState(State.EXPLODE);
	}
}
